package com.jarchie.htgl.fragment.contract;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import com.jarchie.htgl.R;
import com.jarchie.htgl.base.BaseFragment;

/**
 * Created by Jarchie on 2017\12\12.
 * 合同审批的Fragment
 */

public class ConApproveFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
    private TabLayout mTabLayout;
    private String[] mTitles = {"未审批", "已审批"};
    private Fragment alreadyFgt, noFgt;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_con_approve;
    }

    @Override
    protected void initView(View rootView) {
        mTabLayout = obtainView(R.id.mTabLayout);
    }

    @Override
    protected void setListener() {
        mTabLayout.addOnTabSelectedListener(this);
    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            TabLayout.Tab tab = mTabLayout.newTab();
            tab.setText(mTitles[i]);
            mTabLayout.addTab(tab, i);
        }
        setSelect(0);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setSelect(0);
                break;
            case 1:
                setSelect(1);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {}

    @Override
    public void onTabReselected(TabLayout.Tab tab) {}

    //控制切换Fragment的显示
    private void setSelect(int pos) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (pos) {
            case 0: //未审批模块
                if (noFgt == null) {
                    noFgt = new ApproveNoFragment();
                    transaction.add(R.id.id_content, noFgt);
                } else {
                    transaction.show(noFgt);
                }
                break;
            case 1: //已审批模块
                if (alreadyFgt == null) {
                    alreadyFgt = new ApproveAlreadyFragment();
                    transaction.add(R.id.id_content, alreadyFgt);
                } else {
                    transaction.show(alreadyFgt);
                }
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (noFgt != null) {
            transaction.hide(noFgt);
        }
        if (alreadyFgt != null) {
            transaction.hide(alreadyFgt);
        }
    }

}